<template>
  <el-col>
    <el-form :model="form" label-width="120px">
      <el-form-item>
        <p>请选择充电桩编号：</p>
        <p></p>
      </el-form-item>
      <el-form-item>
        <el-radio-group v-model="form.S_id">
          <el-radio label="001">001</el-radio>
          <el-radio label="002"/>
          002
          <el-radio label="003"/>
          003
          <el-radio label="004"/>
          004
          <el-radio label="005"/>
          005
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        选中充电桩：{{ form.S_id }}
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">启动</el-button>
        <el-button type="primary" @click="onSubmit2">关闭</el-button>
        <el-button type="primary" @click="onSubmit3">故障</el-button>
        <el-button type="primary" @click="onSubmit4">恢复</el-button>
      </el-form-item>
    </el-form>
  </el-col>
</template>

<script>
import axios from 'axios';

export default {
  name: 'StationPower',
  data() {
    return {
      form: {
        S_id: '',
        status: -1,
      },
    };
  },
  methods: {
    onSubmit() {
      this.form.status = 1;
      console.log(this.form.status);
      const path = '/api/admin/power_set';
      if (this.form.S_id !== '') {
        axios.post(path, this.form)
          .then((res) => {
            if (res.data.ok) {
              this.$message.success('启动成功');
            } else {
              this.$message.success('启动失败');
            }
          })
          .catch((error) => {
            // eslint-disable-next-line
            console.error(error);
          });
      } else {
        this.$message.success('请选择充电桩编号');
      }
    },
    onSubmit2() {
      this.form.status = 0;
      console.log(this.form.status);
      const path = '/api/admin/power_set';
      if (this.form.S_id !== '') {
        axios.post(path, this.form)
          .then((res) => {
            if (res.data.ok) {
              this.$message.success('关闭充电桩成功');
            } else {
              this.$message.success('关闭充电桩失败');
            }
          })
          .catch((error) => {
            console.error(error);
          });
      } else {
        this.$message.success('请选择充电桩编号');
      }
    },
    onSubmit3() {
      this.form.status = 0;
      console.log(this.form.status);
      const path = '/api/admin/station_failure';
      if (this.form.S_id !== '') {
        axios.post(path, this.form)
          .then((res) => {
            if (res.data.ok) {
              this.$message.success('选中充电桩发生故障');
            } else {
              this.$message.success('请求回复错误');
            }
          })
          .catch((error) => {
            // eslint-disable-next-line no-alert
            alert('请求发送失败');
            console.error(error);
          });
      } else {
        this.$message.success('请选择充电桩编号');
      }
    },
    onSubmit4() {
      this.form.status = 0;
      console.log(this.form.status);
      const path = '/api/admin/station_recovery';
      if (this.form.S_id !== '') {
        axios.post(path, this.form)
          .then((res) => {
            if (res.data.ok) {
              this.$message.success('选中充电桩故障恢复');
            } else {
              this.$message.success('请求回复错误');
            }
          })
          .catch((error) => {
            // eslint-disable-next-line no-alert
            alert('请求发送失败');
            console.error(error);
          });
      } else {
        this.$message.success('请选择充电桩编号');
      }
    },
  },
};

</script>

<style>
</style>
